var mongoose=require('mongoose');
var commentSchema=mongoose.Schema({
    book:{
        type:mongoose.Schema.ObjectId,
        ref:'Book'
    },

    commentCount:Number,//总评论数
    commentCountStr:String,

    goodCount:Number,//好评数
    goodRate:Number,//好评率
    goodCountStr:String,

    generalCount:Number,//中评数
    generalRate:Number,//中评率
    generalCountStr:String,

    comments:[
        {
            score:Number,//评分
            content:String,//内容
            time:{
                type:Date,
                get: function (time) {
                    return [time.getFullYear(),time.getMonth()+1,time.getDate()].join('-')+' '+[time.getHours(),time.getMinutes()].join(':');
                }
            },
            nick:String,
            images:[String],//原始数据
            smalls:[String],//小图片
            bigs:[String]//大图片
        }
    ]
});

commentSchema.statics.page=function(query,fields,opts,cb){
    //console.log('\n-------------------------Comment.page:');
    var page=opts.page||1,limit=opts.limit||20,options;
    fields=Object.assign({sku:true},fields);
    options={
        skip:(page-1)*limit,
        limit:limit
    }
    if(opts.sort)options.sort=opts.sort;
    //console.log('\nquery:',query);
    //console.log('fields:',fields);
    //console.log('\noptions:',options);
    this.find(query,fields,opts,(err,comments)=>{
        //console.log('\ncomments.length:',comments.length);
        //console.log('--------------------------------------------------');
        cb(err,comments);
    });
};

module.exports=mongoose.model('Comment',commentSchema);